package com.link510.aitools.activity.airobot.core;

import android.os.Bundle;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.domain.device.DeviceInfo;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.core.message.MessageInfo;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.services.Airobots;
import com.link510.aitools.services.ApplicationUtils;
import com.link510.aitools.services.CustomWaitDialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class RobotCoreEditNameActivity extends BaseAcitivity {

    /**
     * 加载
     */
    private CustomWaitDialogUtils customWaitDialogUtils;

    @BindView(R.id.name_edit_text)
    EditText nameEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_robot_core_edit_name);
        customWaitDialogUtils = new CustomWaitDialogUtils(this);
        initData();
    }

    private void initData() {

        /*
         * 设备信息
         */
        DeviceInfo deviceInfo = ApplicationUtils.getInstance().getApplication().getDeviceInfo();

        if (deviceInfo == null || Strings.isNullOrEmpty(deviceInfo.getDeviceSN())) {
            ToastUtils.showShort("设备读取异常");
            finish();
            return;
        }

        nameEditText.setText(deviceInfo.getName());
    }

    /**
     * 设备名称修改
     */
    @OnClick(R.id.send_button)
    public void sendButtonClick() {

        try {

            String newName = nameEditText.getText().toString().trim();

            if (Strings.isNullOrEmpty(newName) || newName.length() >= 20) {

                ToastUtils.showShort("设备名称不能为空和超过20个字符");
                return;
            }

            customWaitDialogUtils.show("设备名称修改中...");
            Airobots.getInstance().editName(this.deviceSN, newName, new CWMListener<MessageInfo<String>>() {
                @Override
                public void onSuccess(String message, MessageInfo<String> stringMessageInfo) {

                    customWaitDialogUtils.close();
                    ToastUtils.showShort("设备名称修改成功");
                }

                @Override
                public void onError(int code, String message) {

                    customWaitDialogUtils.close();
                    ToastUtils.showShort("设备名称修改失败");
                }
            });


        } catch (Exception ignored) {

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        customWaitDialogUtils.dismiss();
    }
}